package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.*;
import com.yunxun.coinmessage.entity.*;
import com.yunxun.coinmessage.templatedata.SubscribeTemplateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SubscribeService {
    @Autowired
    SubscribeDao subscribeDao;
    @Autowired
    UserDao userDao;
    @Autowired
    ExchangeDao exchangeDao;
    @Autowired
    CoinDao coinDao;
    @Autowired
    ProgramDao programDao;
    //查询所有订阅条目
    public List<SubscribeTemplateData> querySubscribeByPage(int start, int limit){
        List<SubscribeTemplateData> subscribeTemplateDataList =new ArrayList<SubscribeTemplateData>();//返回给页面subscribe-list的模型
        List<Subscribe> subscribeList= subscribeDao.querySubscribeByPage(start,limit);
        for(Subscribe subscribe:subscribeList){
            subscribeTemplateDataList.add(getSubscribeModel(subscribe));
        }
        return subscribeTemplateDataList;
    }
    //改变订阅的状态
    public Integer  updateSubscribeStatus(Subscribe subscribe){
        return  subscribeDao.updateSubscribeStatus(subscribe);
    }
    //新增订阅
    public Integer addSubscribe(Subscribe subscribe){
        subscribe.setCreate_time(System.currentTimeMillis()/1000);
        return  subscribeDao.addSubscribe(subscribe);
    }
    public  Integer queryTotal(){
        return subscribeDao.queryTotal();
    }
    public List<SubscribeTemplateData> querySubscribeByUid(int uid){
        List<SubscribeTemplateData> subscribeTemplateDataList =new ArrayList<SubscribeTemplateData>();//返回给页面subscribe-list的模型
        List<Subscribe> subscribeList= subscribeDao.querySubscribeByUid(uid);
        for(Subscribe subscribe:subscribeList){
            subscribeTemplateDataList.add(getSubscribeModel(subscribe));
        }
        return subscribeTemplateDataList;
    }

    /**
     * 查询是否有重复订阅
     * @param subscribe
     * @return
     */
    public int queryBySubscribe(Subscribe subscribe){
        return subscribeDao.queryBySubscribe(subscribe);
    }


    /**
     * 组装要返回的订阅模型
     * @param subscribe
     * @return
     */
    public SubscribeTemplateData getSubscribeModel(Subscribe subscribe){
        SubscribeTemplateData subscribeTemplateData =new SubscribeTemplateData();
        User user=userDao.queryById(subscribe.getUid());
        Program program=programDao.queryById(subscribe.getType_id());
        //格式化时间戳
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time=subscribe.getCreate_time()*1000;
        Date date=new Date(time);
        String formatTime=format.format(date);
        subscribeTemplateData.setFormatTime(formatTime);
        subscribeTemplateData.setUser(user);//订阅条目所属用户
        subscribeTemplateData.setProgram(program);//订阅栏目
        subscribeTemplateData.setSub_id(subscribe.getSub_id());
        //根据栏目找到栏目模型
        switch (program.getProgram_id()){
            case 1: //为1时表示市场快讯
            {
                Coin coin=coinDao.queryById(subscribe.getModel_id());
                subscribeTemplateData.setCoin(coin);
                break;

            }
            case 2://为2时表示币种跟踪
            {
                Coin coin=coinDao.queryById(subscribe.getModel_id());
                subscribeTemplateData.setCoin(coin);
                break;

            }
            case 3://为3时表示交易所动态
                Exchange exchange = exchangeDao.queryById(subscribe.getModel_id());
                subscribeTemplateData.setExchange(exchange);
                break;
            default:
                throw new RuntimeException("该订阅条目的栏目未找到");
        }
        return subscribeTemplateData;
    }
    public List<Integer> queryUidByContent(int type_id,int model_id,int start,int limit){
        return subscribeDao.queryUidByCcntent(type_id,model_id,start,limit);
    }
    public int queryCountByContent(int type_id,int model_id){
        return subscribeDao.queryCountByContent(type_id,model_id);
    }
    public int queryQuickNewsTotal(){
        return subscribeDao.queryQuickNewsUserTotal();
    }
    public List<Integer> queryQuickNewsUser(int start,int limit){
        return subscribeDao.queryQuickNewsUser(start,limit);
    }

    /**
     * 判读该用户是否订阅了快讯
     * @param uid
     * @return
     */
    public int judgeSubQuikNews(int uid){
        return subscribeDao.judgeSubQuickNews(uid);
    }

    /**
     * 订阅了交易所的总用户数
     * @return
     */
    public int queryExchangeTotal(){
        return subscribeDao.queryExchangeTotal();
    }

    /**
     * 订阅了交易所的所有用户id
     * @param start
     * @param limit
     * @return
     */
    public List<Integer> queryExchangeUid(int start,int limit){
        return subscribeDao.queryExchangeUid(start,limit);
    }

    /**
     * 判断某个用户是否订阅了该交易所
     * @param uid
     * @param coin_id
     * @return
     */
    public int judgeSubCoin(int uid,int coin_id){
        return subscribeDao.judgeSubCoin(uid,coin_id);
    }
    public int judgeSubExchange(int uid){
        return subscribeDao.judgeSubExchange(uid);
    }
    public int queryExistDatabase(Subscribe subscribe){
        return subscribeDao.queryExistDatabase(subscribe);
    }
    /**
     * 先去用户表中模糊查询找到uidList，然后再到订阅表中遍历uidList找到用户的所有订阅
     * @param uname
     * @return
     */
    public List<SubscribeTemplateData> querySubscribeByUidList(String uname){
        SubscribeTemplateData subscribeTemplateData=new SubscribeTemplateData();
        List<SubscribeTemplateData> subscribeTemplateDataList=new ArrayList<>();
        List<Integer> uidList=userDao.queryUidByUname(uname);
        //如果集合为空,直接返回空的subscribeTemplateDataList
        if(uidList.size()==0){
            return subscribeTemplateDataList;
        }
        List<Subscribe> subscribeList=subscribeDao.querySubscribeByUidList(uidList);
        for(Subscribe subscribe:subscribeList){
            subscribeTemplateDataList.add(getSubscribeModel(subscribe));
        }
        return subscribeTemplateDataList;
    }
}
